package cn.qayy.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import cn.qayy.constants.Constant;
import cn.qayy.service.Uploadable;
/***
 * 文件上传，把文件上传后，从excel里读取数据，并对数据进行验证过滤
 * mlist：存放所有验证不成功的消息提示数据，用于在前台提醒用户
 * list:存放所有成功通过验证的数据，用于保存到数据库
 * @author jobs1127
 *
 */
@SuppressWarnings("all")
public class UploadHandler {
	static Logger log = Logger.getLogger(UploadHandler.class);
	/***
	 * 
	 * @param file 上传的文件
	 * @param clazz 实体类的字节码对象
	 * @return
	 * @throws Exception
	 */
	public static Map<String, List<Uploadable>> getLastData(MultipartFile file, Class clazz) throws Exception{
		//存放Excel文件转换的接口对象，这里存放的对象，没有经过验证
		List<Uploadable> forList = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		//false:给表头,即把Excle文件的表头也获取进来 
		forList.addAll(ExcelUtils.ExcelForList(file, clazz, false));
		//验证每一个对象，并封装成map返回
		return getLastData(forList,clazz);
	}
	/***
	 * 返回一个前台需要的map
	 * @param forList 
	 * @param clazz
	 * @return
	 * @throws Exception
	 */
	public static Map<String, List<Uploadable>> getLastData(List<Uploadable> forList, Class clazz) throws Exception{
		Map<String, List<Uploadable>> map = new HashMap<String, List<Uploadable>>(Constant.INIT_CAPACITY);
		/*** 所有验证不通过的msgInfo **/
		List<Uploadable> mlist = new ArrayList<Uploadable>();
		/*** 所有验证通过的数据;N新增的数据，E修改，D删除 **/
		List<Uploadable> list = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		
		List<Uploadable> listN = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		List<Uploadable> listE = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		List<Uploadable> listD = new ArrayList<Uploadable>(Constant.SMALL_CAPACITY);
		
		/** 过滤数据，并把结果存放到map **/
		Map<String, List<Uploadable>> lmap = FilterData.filter(forList);
		/**用户上传的总记录数**/
		map.put("totalCount", lmap.get("totalCount"));
		
		if (lmap != null && !lmap.isEmpty()) {
			for (String key : lmap.keySet()) {
				if (key.contains("fail") || key.equals("stop")) {
					List<Uploadable> flist = lmap.get(key);
					mlist.addAll(flist);
				} else if (key.contains("sucess")) {
					List<Uploadable> flist = lmap.get(key);
					list.addAll(flist);
				}
			}
		}
		if (!mlist.isEmpty()) {
			map.put("fail", mlist);
		} else {
			for(Uploadable u:list) {
				if(StringUtil.equals("E",u.flag())) {
					listE.add(u);
				} else if(StringUtil.equals("N",u.flag())) {
					listN.add(u);
				} else if(StringUtil.equals("D",u.flag())) {
					listD.add(u);
				}
			}
			map.put("E", listE);
			map.put("N", listN);
			map.put("D", listD);
		}
		return map;
	}
	/***
	 * 
	 * @param Uploadable bean 页面form表单对应的dto bean对象
	 * @return
	 * @throws Exception
	 */
	public static Map<String, List<Uploadable>> getLastData(Uploadable bean) throws Exception{
		Map<String, List<Uploadable>> map = new HashMap<String, List<Uploadable>>(Constant.INIT_CAPACITY);
		/*** 所有验证不通过的msgInfo **/
		List<Uploadable> mlist = new ArrayList<Uploadable>();
		/*** 所有验证通过的数据 N新增的数据，E修改，D删除 **/
		List<Uploadable> list = new ArrayList<Uploadable>(Constant.INIT_CAPACITY);
		List<Uploadable> listN = new ArrayList<Uploadable>(Constant.INIT_CAPACITY);
		List<Uploadable> listE = new ArrayList<Uploadable>(Constant.INIT_CAPACITY);
		List<Uploadable> listD = new ArrayList<Uploadable>(Constant.INIT_CAPACITY);
		/** 过滤数据，并把结果存放到map **/
		Map<String, List<Uploadable>> lmap = FilterData.filter(bean);
		if (lmap != null && !lmap.isEmpty()) {
			for (String key : lmap.keySet()) {
				if (key.contains("fail") || key.equals("stop")) {
					List<Uploadable> flist = lmap.get(key);
					for (Uploadable u : flist) {
						mlist.add(u);
					}
				} else if (key.contains("sucess")) {
					List<Uploadable> flist = lmap.get(key);
					for (Uploadable u : flist) {
						list.add(u);
					}
				}
			}
		}
		if (!mlist.isEmpty()) {/** 验证不通过的数据，前台提示。 */
			map.put("fail", mlist);
		} else {/** 验证通过后，DB持久化。 **/
			for(Uploadable u:list) {
				if(StringUtil.equals("E",u.flag())) {
					listE.add(u);
				} else if(StringUtil.equals("N",u.flag())) {
					listN.add(u);
				} else if(StringUtil.equals("D",u.flag())) {
					listD.add(u);
				}
			}
			map.put("E", listE);
			map.put("N", listN);
			map.put("D", listD);
		}
		return map;
	}
}
